<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
          <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="SystemDictionaryTypes" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" @cell-dblclick="drawer = true">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column prop="id" label="id" width="60">
			</el-table-column>
			<el-table-column prop="name" label="字典名" width="320" sortable >
			</el-table-column>
			<el-table-column prop="sn" label="字典编号" width="300"  sortable>
			</el-table-column>
			<el-table-column align="center" width="150" :render-header="renderHeader" >
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
    <!--分页条-->
    <el-col :span="24" class="toolbar">
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="total"
                     :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
    </el-col>


		<!--新增编辑界面二合一
    visible.sync控制模态框是否显示
    ref模态框别名
    rules 绑定表单校验
    prop 绑定需要校验的行
    :title 绑定单击事件取值
		-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="字典名" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="字典编号" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
    <el-drawer
        title="我是标题"
        :visible.sync="drawer"
        :with-header="false">
      <span>数据字典明细</span>
      <!--列表-->
      <el-table :data="SystemDictionaryDetails" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" label="id" width="60">
        </el-table-column>
        <el-table-column prop="types_id" label="编号" width="320" sortable>
        </el-table-column>
        <el-table-column prop="name" label="明细名" width="300"  sortable>
        </el-table-column>
        <el-table-column align="center" width="150" :render-header="renderHeader" >
          <template scope="scope">
            <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页条-->
      <el-col :span="24" class="toolbar">
        <el-pagination layout="prev, pager, next"
                       @current-change="handleCurrentChange"
                       :page-size="pageSize"
                       :total="total"
                       :current-page="currentPage"
                       style="float:right;">
        </el-pagination>
      </el-col>


    </el-drawer>
	</section>
</template>

<script>

	export default {
		data() {
			return {
        SystemDictionaryDetails: [],
        drawer: false,
        title :"",
				filters: {
        keyword:""
				},
        deptTree:[],//下拉框树形数据接收
        SystemDictionaryTypes: [],
        employees:[],//部门管理员下拉列表数据
				total: 0,
        currentPage: 1,
				pageSize: 5,
				listLoading: false,
				sels: [],//列表选中列
				saveFormVisible: false,//编辑界面是否显示
				saveLoading: false,
				saveFormRules: {
					name: [
						{ required: true, message: '请输入字典名', trigger: 'blur' }
					],
          sn:[
            {required: true, message: '请输入字典编号', trigger: 'blur'}
          ] //给编辑框属性赋值提示
				},
				//编辑界面数据
				saveForm: {
          id:null,
					name: '',
          sn:"",
          state:0,
          manager:null,
          parent:null,
				}
			}
		},
		methods: {
      renderHeader(h) {
        let a = [h('el-button', {
          props: {
            type: "primary"
          },
          on: {
            click: () => {
              this.handleAdd();
            }
          }
        }, '新增')];
        return h('div', a);
      },
      //高级查询
      search(){
      this.currentPage = 1;//高级查询一定要定位到第一页防止数据不够一页用户查完还在查询页看不到数据
      this.getSystemDictionaryTypes();
      },

			handleCurrentChange(val) {
				this.currentPage = val;
				this.getSystemDictionaryTypes();
			},
			//获取部门列表
			getSystemDictionaryTypes() {
				let para = {
					currentPage: this.currentPage,
          pageSize:this.pageSize,
          keyword: this.filters.keyword  //高级查询关键字
				};
        this.listLoading = true;//开启忙等框
        //发送axios请求从后端8080拿数据
        this.$http.post("/obtain/page",para)
            .then(result=>{ //请求成功
              this.listLoading = false; //关闭忙等框
              console.log(result);
              result = result.data;
              this.SystemDictionaryTypes = result.rows;
              this.total = result.total;
            })
            .catch(result=>{
              console.log(result);
              this.$message({
                type: 'error',
                message: "系统繁忙中"
              });
            })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/obtain/"+row.id)
          .then(result=>{
            this.listLoading = false; // 关闭忙等框
            result = result.data;
            if (result.success){//删除成功
              this.$message({
                type:"success",
                message:result.message //获取前端返回的信息
              });
              this.currentPage = 1;
              this.getSystemDictionaryTypes() //定位到首页重新加载页面
            }else {//删除失败
              this.$message({
                type:"error",
                message:result.message
              });
              this.currentPage = 1;
              this.getSystemDictionaryTypes() //定位到首页重新加载页面
            }
          }).catch(result=>{ //操作异常
            this.listLoading = false;
            console.log(result);
            this.$message({
              type: 'error',
              message: '系统错误,正在殴打程序员'
            });
          }).catch(()=>{//点击取消按钮 无操作

          })
        })
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.getDeptTree();//点击按钮加载部门树
        this.title = "修改";
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);//回显
			},
			//显示新增界面
			handleAdd: function () {
        this.getDeptTree(); //点击按钮加载部门树
        this.title = "添加数据字典";
				this.saveFormVisible = true;
				this.saveForm = {
          id:null,
          name: '',
          sn:"",
				};
			},
			//编辑
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.saveLoading = true;//开启按钮忙等
              this.listLoading = true;//开启忙等框
              let para = Object.assign({}, this.saveForm); //获取模态框中的数据
              if(para.parent){
                para.parent = {"id":para.parent[para.parent.length-1]}//转换前端部门树形数据id的格式只取最后一位
              }
              //发送请求
              this.$http.post("/obtain/save",para)
                  .then(result=>{
                    this.listLoading = false; // 关闭忙等框
                    result = result.data;
                    if (result.success){//新增成功
                      this.saveLoading = false;//关闭按钮忙等
                      this.$message({
                        type:"success",
                        message:result.message //获取前端返回的信息
                      });
                      this.currentPage = 1;
                      this.getSystemDictionaryTypes() //定位到首页重新加载页面
                      this.saveFormVisible = false;//关闭模态框
                    }else {//失败
                      this.$message({
                        type:"error",
                        message:result.message
                      });
                      this.currentPage = 1;
                      this.getSystemDictionaryTypes() //定位到首页重新加载页面
                    }
                  }).catch(result=>{ //操作异常
                this.listLoading = false;
                console.log(result);
                this.$message({
                  type: 'error',
                  message: '系统错误,正在殴打程序员'
                });
              }).catch(()=>{//点击取消按钮 无操作

              })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels; //勾选复选框的时候触发，当前复选框中所有的值
			},
      //下拉选项框
      getEmployees(){
        this.$http.get("/employee/list")
            .then(result=>{
              this.employees = result.data; //把前端查询到的数据赋值给employess方便下拉框取值
            })
      },
      getDeptTree(){
        this.$http.post("/dept/tree")
        .then(result=>{
          this.deptTree = result.data;
        })
      }
		},
		mounted() {
			this.getSystemDictionaryTypes(); /*页面加载时触发   钩子函数*/
      this.getEmployees(
      ); /*下拉框*/
		}
	}

</script>

<style scoped>

</style>